package com.yt.lxh.acl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yt.lxh.acl.service.AdminService;
import com.yt.lxh.common.result.Result;
import com.yt.lxh.common.result.ResultCodeEnum;
import com.yt.lxh.common.service.MinioService;
import com.yt.lxh.common.utils.JwtUtils;
import com.yt.lxh.dto.admin.AdminDto;
import com.yt.lxh.dto.admin.AssginRoleDto;
import com.yt.lxh.model.acl.Admin;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;

@Api(tags = "系统管理员接口")
@RequestMapping("/admin/acl/user")
//@CrossOrigin
@RestController
public class AdminController {

    @Autowired
    private AdminService adminService;

    @ApiOperation("管理员列表")
    @PostMapping("/findByPage/{current}/{limit}")
    public Result list(@PathVariable Long current,
                       @PathVariable Long limit,
                       @RequestBody  AdminDto adminDto){
        Page<Admin> pageParams = new Page<>(current, limit);
        IPage<Admin> pageModel = adminService.findByPage(pageParams, adminDto);
        return Result.success(pageModel);
    }

    @ApiOperation("添加管理员")
    @PostMapping("addAdmin")
    public Result addAdmin(@RequestBody Admin admin,
                           @RequestHeader(value = "token") String token){
        adminService.addAdmin(admin,token);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @ApiOperation("根据管理员名字查询管理员数据")
    @PostMapping("findByAdminName")
    public Result findByAdminName(String username){
        Admin adminByUsername = adminService.findByAdminName(username);
        return Result.success(adminByUsername);
    }

    @ApiOperation("管理员信息")
    @GetMapping("/info")
    public Result info(String token){
        Integer adminId = JwtUtils.verifyAdminTokenAndGetId(token);
        Admin admin = adminService.getById(adminId);
        HashMap<String,String> map = new HashMap<>();
        map.put("name",admin.getUsername());
        map.put("avatar","https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F201910%2F01%2F20191001163553_smpul.thumb.400_0.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1699679878&t=e4c88c811c633d97d91232faaa33f336");
        return Result.success(map);
    }

    @Autowired
    private MinioService minioService;
    @ApiOperation("文件上传")
    @PostMapping("upload")
    public Result info(@RequestParam("file")MultipartFile multipartFile){
        String filename  =minioService.uploadByFile(multipartFile);
        return Result.build(filename, ResultCodeEnum.SUCCESS);
    }

    @ApiOperation("管理员分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleDto assginRoleDto,@RequestHeader(value = "token") String token){
        adminService.doAssign(assginRoleDto,token);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @ApiOperation("管理员修改")
    @PutMapping("/updateAdmin")
    public Result updateAdmin(@RequestBody Admin admin){
        adminService.updateAdmin(admin);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @ApiOperation("管理员删除")
    @DeleteMapping("/deleteById/{adminId}")
    public Result deleteById(@PathVariable("adminId") Integer adminId){
        adminService.deleteById(adminId);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }
}
